layui.use(['jquery', 'layer'], function () {
  var form = layui.form;
  var $ = layui.jquery;
  var layer = layui.layer;

  //今日签到
  $('#LAY_signin').on('click', function () {
    if ($('#isLogin').length > 0) {
      $.ajax({
        url: '/user/sign',
        type: 'post',
        success(data) {
          if (data.code == 0) {
            layer.msg(data.message)
            $('#LAY_signin').after(
                '<button class="layui-btn layui-btn-disabled">今日已签到</button><span>获得了<cite>'
                + data.data + '</cite>飞吻</span>')
            $('#LAY_signin').remove()
            $('#LAY_signin_get').remove()
          } else {
            layer.msg(data.message)
          }
        }
      })
    } else {
      layer.msg('请登录后再签到')
    }
  })

  //签到说明
  $('#LAY_signinHelp').on('click', function () {
    layer.open({
      type: 1
      , title: '签到说明'
      , area: '300px'
      , shade: 0.8
      , shadeClose: true
      , content: ['<div class="layui-text" style="padding: 20px;">'
        ,
        '<blockquote class="layui-elem-quote">“签到”可获得社区飞吻，规则如下</blockquote>'
        , '<table class="layui-table">'
        , '<thead>'
        , '<tr><th>连续签到天数</th><th>每天可获飞吻</th></tr>'
        , '</thead>'
        , '<tbody>'
        , '<tr><td>＜5</td><td>5</td></tr>'
        , '<tr><td>≥5</td><td>10</td></tr>'
        , '<tr><td>≥15</td><td>15</td></tr>'
        , '<tr><td>≥30</td><td>20</td></tr>'
        , '</tbody>'
        , '</table>'
        , '<ul>'
        , '<li>中间若有间隔，则连续天数重新计算</li>'
        , '<li style="color: #FF5722;">不可利用程序自动签到，否则飞吻清零</li>'
        , '</ul>'
        , '</div>'].join('')
    });
  });

  $('#LAY_signinTop').on('click', function () {
    var loadIndex = layer.load(1, {shade: 0.8});
    fly.json('../json/signin.js', function (res) { //实际使用，请将 url 改为真实接口
      var tpl = $(
          ['<div class="layui-tab layui-tab-brief" style="margin: 5px 0 0;">'
            , '<ul class="layui-tab-title">'
            , '<li class="layui-this">最新签到</li>'
            , '<li>今日最快</li>'
            , '<li>总签到榜</li>'
            , '</ul>'
            ,
            '<div class="layui-tab-content fly-signin-list" id="LAY_signin_list">'
            , '<ul class="layui-tab-item layui-show"></ul>'
            , '<ul class="layui-tab-item">2</ul>'
            , '<ul class="layui-tab-item">3</ul>'
            , '</div>'
            , '</div>'].join(''))
          , signinItems = tpl.find('.layui-tab-item');

      layer.close(loadIndex);

      layui.each(signinItems, function (index, item) {
        var html = laytpl(tplSigninTop).render({
          data: res.data[index]
          , index: index
        });
        $(item).html(html);
      });

      layer.open({
        type: 1
        , title: '签到活跃榜 - TOP 20'
        , area: '300px'
        , shade: 0.8
        , shadeClose: true
        , id: 'layer-pop-signintop'
        , content: tpl.prop('outerHTML')
      });

    }, {type: 'get'});
  });

});
